﻿<?php
require('configs/include.php');

class c_descargar_kernel extends super_controller {

    public function descargar() {
        $version = $this->post->version;
        if (!is_empty($version)) {
            $file = C_FULL_PATH . "recursos/kernel/" . $version . ".zip";
            if (file_exists($file)) {
                $this->descargar_archivo($file);
            } else {
                throw_exception("Error al intentar descargar");
            }
        } else {
            throw_exception("Debe seleccionar la versión del kernel a descargar");
        }
    }

    function descargar_archivo($archivo, $downloadfilename = null) {
        $downloadfilename = $downloadfilename !== null ? $downloadfilename : basename($archivo);
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . $downloadfilename);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($archivo));

        ob_clean();
        flush();
        readfile($archivo);
        exit;
    }

    public function display() {

        $options['kernel']['lvl2'] = "by_stateF";

        $this->orm->connect();
        $this->orm->read_data(array("kernel"), $options);
        $kernels = $this->orm->get_objects("kernel");
        $this->orm->close();

        $this->engine->assign('kernel', $kernels);
        $this->engine->assign('ubicationPath', $this->gvar['n_update']);
        $this->engine->display('header.tpl');
        $this->engine->display($this->temp_aux);
        $this->engine->display('descargar_kernel.tpl');
        $this->engine->display('footer.tpl');
    }

    public function run() {
        if ($_SESSION['usuario'] != "") {
            try {
                if (isset($this->get->option)) {
                    $this->{$this->get->option}();
                }
            } catch (Exception $e) {
                $this->error = 1;
                $this->msg_warning = $e->getMessage();
                $this->img_warning = "incorrect.png";
                $this->engine->assign('type_warning', $this->type_warning);
                $this->engine->assign('msg_warning', $this->msg_warning);
                $this->engine->assign('img_warning', $this->img_warning);
                $this->temp_aux = 'message.tpl';
            }
            $this->display();
        } else {
            $this->engine->display('header.tpl');
            $this->engine->display('sinPermisos.tpl');
            $this->engine->display('footer.tpl');
        }
    }

}

$call = new c_descargar_kernel();
$call->run();
?>